package http;

import frame.service.standard.Constants;
import org.springframework.web.servlet.view.AbstractView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.util.Map;

/**
 * 默认视图
 * */
public class DefaultView extends AbstractView {
    //默认返回消息内容
    public static String DEFAULT_RET = "SUCCESS";

    @Override
    protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //默认视图返回字节数组，信息来源：ServiceContext中respData的Constants.RESPONSE_MSG_CONTENT属性值
        Object content = map.get(Constants.RESPONSE_MSG_CONTENT);

        byte[] response = null;
        if(content == null){
            response = DEFAULT_RET.getBytes();
        }else if(content instanceof byte[]){
            response = (byte[])content;
        }else{
            response = content.toString().getBytes();
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(response, 0, response.length);
        super.writeToResponse(httpServletResponse, byteArrayOutputStream);
    }
}
